/**
 * @file
 * @auth jinguangguo
 * @date 2017/5/11
 */

import Vue from 'vue';

import filter from '../../../../grsc-base/util/filter';
import commonService from '../../../common/commonService';
import service from './service'
import MathTool from '../../../../grsc-base/util/mathtool';
import assetConfig from '../../../common/assetConfig';

export default Vue.extend({
    data() {
        return {
            precision: 0,
            filter: filter,
            coinBalance: '--',
            coinLock: '--',
            btcPrice: '--',
            usdPrice: '--',
            cnyPrice: '--',
            coinType: ''
        };
    },
    created() {
        // this.precision = +this.$$assetMap[this.coinType][assetConfig.WITHDRAWPRECISION];
        this.fetchCoinList();
        this.getTotalAssets();
        $(document).off('$$USDCNY').on('$$USDCNY', (event, USDCNY) => {
            this.$USDCNY = USDCNY;
            this.$forceUpdate();
        });
    },
    watch: {

    },
    methods: {

        /**
         * 获取开放充值个提现的列表
         * */
        fetchCoinList() {
            let that = this;
            let name = this.$router.currentRoute.name;
            let coinList = [];
            if (name === 'deposit') {
                // 充值开启的列表
                $.map(this.$$assetList, (item, index) => {
                    // 0:开启 -1:关闭
                    if (+item[assetConfig.DEPOSITLEVEL_DEFAULT] === 0) {
                        coinList.push(item.assetCode);
                    }
                });
                this.coinList = coinList;
                
                localStorage.setItem('cointype1',coinList[0])
                _doCoinType();
            } else if (name === 'withdraw') {
                service.withdrawAssetList().done(rep => {
                    if (rep.code === window.CODE.SUCCESS) {
                        let results = [];
                        let withdrawList = rep.data;
                        $.map(this.$$assetList, (item, index) => {
                            $.map(withdrawList, (innerAssetCode, innerIndex) => {
                                if (item.assetCode === innerAssetCode) {
                                    results.push(item.assetCode);
                                }
                            });
                        });
                        this.coinList = results;
                        if(!localStorage.getItem('cointype2')) {
                            localStorage.setItem('cointype2',results[0]);
                            _doCoinType();
                        } else {
                            that.coinType = localStorage.getItem('cointype2');
                        }
                       
                    }
                })
            }

            function _doCoinType() {
                // that.coinList.sort();
                that.coinType = that.coinList[0];
            }
        },

        /**
         * 获取当前币种价格、可用、冻结
         * */
        getTotalAssets() {
            let that = this;
            // commonService.getBitfinexBtcPrice(), btcPriceRep, 
            $.when(commonService.getTradeDetail(),
                commonService.getTotalAssets({
                    assetCode: this.coinType
                })).done((detailRep, assetRep) => {
                // if (btcPriceRep.code === window.CODE.SUCCESS) {
                //     this.btcPrice = btcPriceRep.data.last_price;
                // } else {
                //     window.error(btcPriceRep.msg);
                // }

                _doCurrentPrice(detailRep);

                if (assetRep.code === window.CODE.SUCCESS) {
                    _doAssetData(assetRep.data);
                } else {
                    window.error(assetRep.msg);
                }
            });

            //获取当前资产对应的btc价格
            function _doCurrentPrice(data) {
                let usd = 0;
                if (data['BTC_' + that.coinType]) {
                    let btc = 0;
                    btc = data['BTC_' + that.coinType]['newPrice'];
                    usd = MathTool.multiplication(btc, that.btcPrice);
                } else if (data['USDT_' + that.coinType]) {
                    usd = data['USDT_' + that.coinType]['newPrice'];
                } else if (data['USC_' + that.coinType]) {
                    usd = data['USC_' + that.coinType]['newPrice'];
                }
                that.usdPrice = filter.floorFixPrice(usd, 2);
                let CURRENCY_RMB = that.coinType === 'UDC' ? that.CURRENCY_RMB : that.$USDCNY;
                that.cnyPrice = filter.floorFixPrice(MathTool.multiplication(usd, CURRENCY_RMB), 2);
            }

            function _doAssetData(data) {
                $.map(data, (item, index) => {
                    if (item.assetCode === that.coinType) {
                        that.coinBalance = filter.floorFixPrice(item.amountAvailable, that.precision);
                        that.coinLock = filter.floorFixPrice(item.amountLock, that.precision);

                        if (that.rules && that.rules.amount) {
                            let newTip = that.$t('asset.available') + ': ' + filter.floorFixPrice(item.amountAvailable, that.precision) + ' ' + that.coinType;
                            that.rules.amount.tip = newTip;
                            that.rules.amount.max = item.amountAvailable;
                            that.rules.amount.maxTip = newTip;
                            that.rules.amount.$setRight(newTip);
                        }
                    }
                });
            }
        },
    }
});